package com.muyi.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.muyi.pojo.entity.Category;
import com.muyi.pojo.entity.Result;
import com.muyi.service.CategoryService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Api(tags = "分类模块")
@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询分类数据
     * @return
     */
    @ApiOperation(value = "查询全部数据")
    @GetMapping
    public Result list(){
        log.info("查询全部分类");
        //调用service查询分类数据
        List<Category> CategoryList =  categoryService.list();
        return Result.success(CategoryList);
    }


    /**
     * 删除分类
     * @return
     */
    @ApiOperation(value = "删除分类")
    @DeleteMapping("/{category_id}")
    public Result delete(@ApiParam(value = "分类ID", required = true) @PathVariable Integer category_id){
        log.info("根据category_id删除分类:{}",category_id);
        //调用service删除分类
        categoryService.delete(category_id);
        return Result.success();
    }


    /**
     * 新增分类
     * @return
     */
    @ApiOperation(value = "新增分类")
    @PostMapping
    public Result add(@ApiParam(value = "分类对象", required = true) @RequestBody Category category){
        log.info("新增分类: {}" , category);
        //调用service新增分类
        categoryService.add(category);
        return Result.success();
    }




    @ApiOperation(value = "更新分类")
    @PutMapping
    public Result update(@ApiParam(value = "分类对象", required = true) @RequestBody Category Category){
        log.info("更新分类信息 : {}", Category);
        categoryService.update(Category);
        return Result.success();
    }

    // 根据category_id查询分类
    @ApiOperation(value = "根据分类id查询分类")
    @GetMapping("/{category_id}")
    public Result getByCategoryId(@ApiParam(value = "分类ID", required = true) @PathVariable Integer category_id){
        log.info("根据category_id查询分类, category_id:{}",category_id);
        Category Category = categoryService.getByCategoryId(category_id);
        return Result.success(Category);
    }
}
